﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Net;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using ConexionAgente;
using System.Runtime.Serialization.Formatters;
using System.Collections;

namespace Agente
{
    class MessageHandler
    {
        private Config conf = null;
        private ServicioAgente servicioRemoto;

        public MessageHandler(Config conf)
        {
            this.conf = conf;
        }

        public void IniciarServicio()
        {

            servicioRemoto = new ServicioAgente();


            BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider();
            serverProv.TypeFilterLevel = TypeFilterLevel.Full;
            RemotingConfiguration.CustomErrorsMode = CustomErrorsModes.Off;
            serverProv.TypeFilterLevel = TypeFilterLevel.Full;

            IDictionary propBag = new Hashtable();
            propBag["port"] = conf.Puerto_agente;
            propBag["typeFilterLevel"] = TypeFilterLevel.Full;
            propBag["name"] = "AgentChannel";
            TcpChannel tcpChan = new TcpChannel(propBag, null, serverProv);
            ChannelServices.RegisterChannel(tcpChan, false);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServicioAgente), "Agente", WellKnownObjectMode.Singleton);

            Console.WriteLine("Servicio levantado en el puerto " + conf.Puerto_agente + "..");
            Console.Read();
        }
    }
}
